package com.yixun.service.impl;

import com.alibaba.fastjson.JSON;
import com.yixun.entity.YxRole;
import com.yixun.entity.YxUser;
import com.yixun.mapper.RoleMapper;
import com.yixun.service.LogSetService;
import com.yixun.service.RoleService;
import com.yixun.util.FormatUtil;
import com.yixun.util.LogUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


@Service
public class RoleServiceImpl implements RoleService {

    private final RoleMapper roleMapper;

    private final LogSetService logSetService;

    public RoleServiceImpl(RoleMapper roleMapper, LogSetService logSetService) {
        this.roleMapper = roleMapper;
        this.logSetService = logSetService;
    }


    @Override
    public List<YxRole> getAllRoleList() {
        return roleMapper.getAllRoleList();
    }

    @Override
    public String getRoleNameByUserId(String user_id) {
        return roleMapper.getRoleNameByUserId(user_id);
    }

    @Override
    public List<YxRole> getRoleList(Map map) {
        return roleMapper.getRoleList(map);
    }

    @Override
    public String getRoleCount(Map map) {
        return roleMapper.getRoleCount(map);
    }

    @Override
    @Transactional
    public int insertRole(YxUser sessionUser, YxRole role) {
        int row;
        role.setRole_id(new FormatUtil().getUUID());
        row = roleMapper.insertRole(role);
        if (row == 1) {
            logSetService.insertSettingLogs(LogUtil.ADD, "角色", JSON.toJSONString(role), sessionUser);
            return row;
        }
        return row;
    }

    @Override
    public YxRole getRoleBean(String role_id) {
        return roleMapper.getRoleBean(role_id);
    }

    @Override
    @Transactional
    public int updateRole(YxUser sessionUser, YxRole role) {
        int row;
        row = roleMapper.updateRole(role);
        if (row == 1) {
            logSetService.insertSettingLogs(LogUtil.UPDATE, "角色", JSON.toJSONString(role), sessionUser);
            return row;
        }
        return row;
    }

    @Override
    @Transactional
    public int delRole(YxUser sessionUser, Map map) {
        int row;
        row = roleMapper.delRole(map);
        if (row > 0) {
            logSetService.insertSettingLogs(LogUtil.DELETE, "角色", JSON.toJSONString(map), sessionUser);
            return row;
        }
        return row;
    }
}
